﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.IService;
using Microsoft.AspNetCore.Http.Features;
using zhangmenren.Untils;
using zhangmenren.Models.help;
using zhangmenren.ViewModel;
using zhangmenren.Models;
using System.IO;
using System.Text;

namespace zhangmenren.Controllers
{
    public class MapController : Controller
    {
        private IProductService productService;
        private IItemService itemService;
        public MapController(IItemService _itemService, IProductService _productService)
        {
            productService = _productService;
            itemService = _itemService;
        }
        // GET: /<controller>/
        public async Task <IActionResult> Index()
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Redirect("/Login/Index");
            }
            var list = await productService.FindByUserId(user.Id);
            return View(list);
        }

        public IActionResult Map()
        {
            return View();
        }


        public async Task< IActionResult> Detail(int id)
        {
            var item = await itemService.FindById(id);
            return View(item.FirstOrDefault());
        }


        /// <summary>
        /// 查看点
        /// </summary>
        /// <returns>The map.</returns>
        public IActionResult Maps(int id)
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Redirect("/Login/Index");
            }
            if (id==0)
            {
                return Redirect("/Map/Index");
            }
            return View();
        }

        public async Task<JsonResult> GetMaps(int id)
        {
            var list = await itemService.FindByProductId(id);

            return Json(list);
        }


        /// <summary>
        /// 添加点内容
        /// </summary>
        /// <returns>The map.</returns>
        public async Task<IActionResult> Add(int id)
        {

            var list = await itemService.FindByProductId(id);
            return View(list.Count());
        }

        public async Task<JsonResult> AddProduct(ItemEntity entity)
        {
            if (entity.ProductId!=0&&!string.IsNullOrEmpty(entity.Lng) && !string.IsNullOrEmpty(entity.Lat))
            {
                if (string.IsNullOrEmpty(entity.MiaoShu))
                {
                    entity.MiaoShu="无";
                }
                var result = await itemService.Add(entity);
                return Json(new { state = 1, msg = "添加成功" });
            }
            return Json(new { state = 0, msg="数据有误，请返回重新添加" });
        }



        [HttpPost]
        public async Task <JsonResult> Adds(string title)
        {
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
            if (user == null)
            {
                return Json(new {state=2,url= "/Login/Index" });
            }
            if (!string.IsNullOrEmpty(title) )
            {
                ProductsEntity model = new ProductsEntity();
                model.Title = title;
                model.UserId = user.Id;

                var result = await productService.Add(model);

                return Json(new { state = 1, msg="添加成功" });
            }
            return Json(new { state = 0, msg="名称为空" });
        }

    }
}
